home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / prg_casm / jlvesa11.zip / JLVESA20.ASM < prev    next >
Assembly Source File  |  1995-12-19  |  2KB  |  89 lines

  1. ; This file is part of JLVESA SVGA-library
  2. ;
  3. ; Copyright 1995 Johannes Lehtinen
  4. ; All rights reserved
  5.  
  6. model large,c
  7. p386
  8.  
  9. segment jlvesa20_TEXT USE16 'CODE'
  10. assume cs:jlvesa20_TEXT
  11.  
  12. ; int JVColor_Find(JVPalette palette, JVUByte red, JVUByte green, JVUByte blue)
  13. ;
  14. ; Finds best fitting color
  15.  
  16. best_color     dw    ?
  17. best_distance  dd    ?
  18.  
  19. proc JVColor_Find far
  20.    public JVColor_Find
  21.  
  22.    push  bp
  23.    mov   bp,sp
  24.    push  si
  25.    push  di
  26.    push  ds
  27.  
  28.    ; Load address of palette
  29.  
  30.    lds   si,[ss:bp+6]
  31.    cld
  32.  
  33.    ; Go through colors
  34.  
  35.    xor   bx,bx                ; BX is color being handled
  36.    mov   [cs:best_distance],0ffffH ; Set distance to maximum value
  37.  
  38. find_loop:
  39.    xor   ecx,ecx              ; ECX is distance
  40.    mov   di,10                ; Offset of color value
  41.  
  42.    ; Go through color arguments
  43.  
  44. inner_loop:
  45.    xor   eax,eax              ; EAX is value of color being handled
  46.    xor   edx,edx              ; EDX is value of color searched
  47.    lodsb
  48.    mov   dl,[ss:bp+di]
  49.  
  50.    sub   eax,edx              ; Calculate difference
  51.    imul  eax                  ; Difference to second power
  52.    add   ecx,eax              ; Add difference to second power to ECX
  53.    add   di,2
  54.    cmp   di,14                ; Check if values left to check
  55.    jbe   short inner_loop
  56.  
  57.    ; Check if color is closer to wanted than previous best. If it is
  58.    ; mark current color as best available.
  59.  
  60.    cmp   [cs:best_distance],ecx
  61.    jbe   short is_not_closer
  62.  
  63.    mov   [cs:best_color],bx
  64.    mov   [cs:best_distance],ecx
  65.    or    ecx,ecx
  66.    jz    short loop_end
  67.  
  68. is_not_closer:
  69.    inc   bx
  70.    cmp   bx,256
  71.    jb    short find_loop
  72.  
  73.    ; Return nearest color
  74.  
  75. loop_end:
  76.    mov   ax,[cs:best_color]
  77.    pop   ds
  78.    pop   di
  79.    pop   si
  80.    pop   bp
  81.    retf
  82.  
  83. endp JVColor_Find
  84.  
  85. ends
  86.  
  87. end
  88.  
  89.